package cn.doitedu.hbase.day01;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

public class DDLDemo {


    public static void main(String[] args) throws Exception {

        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","node-1.51doit.cn:2181,node-2.51doit.cn:2181,node-3.51doit.cn:2181");
        // 创建一个hbase的客户端连接
        Connection conn = ConnectionFactory.createConnection(conf);
        // 拿到一个DDL的操作工具(创建表，修改版、删除表)
        Admin admin = conn.getAdmin();

        //创建一个表
        //admin.addColumnFamily(TableName.valueOf("t1"), new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(Bytes.toBytes("f1")));
        //admin.createTable(new TableDescriptorBuilder.ModifyableTableDescriptor(TableName.valueOf("t2")).setColumnFamily(new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(Bytes.toBytes("f2"))));

        //指定第一个列族
        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder("f1".getBytes());
        columnFamilyDescriptorBuilder.setInMemory(true);
        columnFamilyDescriptorBuilder.setMaxVersions(5);
        ColumnFamilyDescriptor familyDescriptor1 = columnFamilyDescriptorBuilder.build();

        //第二个列族的信息
        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder2 = ColumnFamilyDescriptorBuilder.newBuilder("f2".getBytes());
        columnFamilyDescriptorBuilder2.setMaxVersions(3);
        ColumnFamilyDescriptor familyDescriptor2 = columnFamilyDescriptorBuilder2.build();

        //指定表的信息
        TableName tableName = TableName.valueOf("tb_users3");
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
        //设置列族
        tableDescriptorBuilder.setColumnFamily(familyDescriptor1);
        tableDescriptorBuilder.setColumnFamily(familyDescriptor2);

        TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
        admin.createTable(tableDescriptor);



        admin.close();

    }
}
